package com.jin.flashsaled.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.jin.flashsaled.entity.SeckillOrder;
import com.jin.flashsaled.mapper.SeckillOrderMapper;
import com.jin.flashsaled.service.ISeckillOrderService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 靳鹏宇
 * @since 2025-09-18
 */
@Service
@Slf4j
public class SeckillOrderServiceImpl extends ServiceImpl<SeckillOrderMapper, SeckillOrder> implements ISeckillOrderService {

    @Autowired
    private SeckillOrderMapper seckillOrderMapper;
    @Autowired
    RedisTemplate redisTemplate;

    @Override
    public SeckillOrder haveBUy(Long id, Long goodsId) {

        // 直接查redis，虽然担心redis缓存与数据库不一致，但哪怕放过去，也会被数据库拒绝插入。



        SeckillOrder seckillOrder=(SeckillOrder) redisTemplate.opsForValue().get("seckillOrder:"+id+goodsId);

        //已经购买了，恢复redis预扣库存
        if(seckillOrder!=null)
        {
            //自增
            redisTemplate.opsForValue().increment("seckillGoods:"+goodsId,1 );
        }

        //查数据库
//        LambdaQueryWrapper<SeckillOrder> wrapper = new LambdaQueryWrapper<>();
//        wrapper.eq(SeckillOrder::getUserId, id).eq(SeckillOrder::getGoodsId, goodsId);
//        seckillOrderMapper.selectOne(wrapper);
        return seckillOrder;
    }
}
